<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>敏感词管理</title>
  <link rel="shortcut icon" href="/img/favicon.ico"/>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="/common/layuimini/layui.css" media="all">
  <link rel="stylesheet" href="/common/layuimini/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
  <div class="layuimini-main">
    <fieldset class="table-search-fieldset">
      <legend>搜索信息</legend>
      <div style="margin: 10px 10px 10px 10px;">
        <div class="layui-form layui-form-pane">
          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">敏感词</label>
              <div class="layui-input-inline">
                <input type="text" name="sensitiveWord" autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">创建人</label>
              <div class="layui-input-inline">
                <input type="text" name="createPeople" autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-inline">
              <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
            </div>
          </div>
        </div>
      </div>
    </fieldset>
    <table id="sensitiveWordsTable" lay-filter="sensitiveWordsTableFilter"></table>

    <!--表格右侧按钮-->
    <script type="text/html" id="toolBar">
      <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
      <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
    </script>
  </div>
</div>
</body>
<script type="text/html" id="currentTableBar" lay-filter="currentTableBar">
  <!--表格顶部按钮-->
  <div class="layui-btn-container">
    <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
    <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
    <!--        <button class="layui-btn layui-btn-sm layui-btn-primary layui-border-green " lay-event="import"> 导入 </button>-->
  </div>
</script>
<script type="text/javascript" src="/common/layui/layui.js"></script>
<script type="text/javascript" src="/common/jquery-1.11.3.js"></script>
<script type="text/javascript" src="/common/xm-select/xm-select.js"></script>
<script type="text/javascript" src="/common/blog-ui.js"></script>
<script type="text/javascript">
  layui.use(['table','form','jquery'],function (){
    var table = layui.table;
    var form = layui.form;
    //数据表格
    table.render({
      elem: "#sensitiveWordsTable",
      url: "/sensitive/admin/tableList",
      toolbar: "#currentTableBar",
      id: "currentSensitiveWordsTable",
      cols: [
        [
          {type: 'checkbox'},
          {field: '', title: '序号',width:60,templet:function (d){
              return d.LAY_INDEX;
            }},
          {field: 'value', title: '内容',width:280},

          {field: 'enable', title: '是否启用',event:'changeSensitiveState', width: 130,templet(d){
              if (d.enable === 1){
                return '<input type="checkbox" value="'+d.id+'" checked="" name="open" lay-skin="switch" lay-filter="switchTest" lay-text="ON|OFF">'
              }else {
                return '<input type="checkbox" value="'+d.id+'" name="open" lay-skin="switch" lay-filter="switchTest" lay-text="ON|OFF">'
              }
            }},
          {field: 'username', title: '创建人账号', width: 180},
          {field: 'nickname', title: '创建人昵称', width: 180},
          {field: 'createTime', title: '创建时间', width: 180},
          {field: 'updateTime', title: '修改时间', width: 180},
          {fixed: 'right', title:'操作', toolbar: '#toolBar', width:150}
        ]
      ],
      limits: [10, 15, 20, 25, 50, 100],
      limit: 10,
      page: true,
      skin: 'line',
      parseData: function(res){//将原始数据解析成 table 组件所规定的数据
        var result;
        if(this.page.curr){
          result = res.data.slice(this.limit*(this.page.curr-1),this.limit*this.page.curr);
        }
        else{
          result=res.data.slice(0,this.limit);
        }
        return {
          "code": res.code, //解析接口状态
          "msg": res.msg, //解析提示文本
          "count": res.count, //解析数据长度
          "data": result //解析数据列表
        };
      }
    })

    //头工具栏事件
    table.on('toolbar(sensitiveWordsTableFilter)', function(obj){
      if (obj.event === 'add') {  // 监听添加操作
        const url = "/sensitive/admin/sensitive-add";
        Pt.openModalBox("新增敏感词",url,"40%","40%");
      }else if (obj.event === 'delete'){
        var allData = table.checkStatus('currentSensitiveWordsTable').data;
        var ids = [];
        for (var i = 0; i < allData.length; i++) {
          ids[i] = allData[i].id;
        }
        //删除敏感词通过ids
        deleteSensitive(ids);
      }
    });


    form.on('submit(data-search-btn)', function (data) {
      table.reload('currentSensitiveWordsTable', {
        url: "/sensitive/admin/tableList",
        where: {
          "sensitiveWord": data.field.sensitiveWord,
          "createPeople": data.field.createPeople
        } //设定异步数据接口的额外参数
      });
    })


    /**
     * 删除敏感词
     * @param ids
     * @return
     */
    function deleteSensitive(ids){
      if (ids === undefined) {
        return;
      }
      layer.confirm('真的删除行么', function (index) {
        $.ajax({
          url: '/sensitive/admin/deleteSensitive',
          contentType: "application/json",
          data: JSON.stringify(ids),
          type: 'post',
          dataType: 'json',
          success: function (res) {
            if (res.state === true) {
              layui.table.reload('currentSensitiveWordsTable');
            } else {
              Pt.showErrorMsg(res.msg)
            }
          }
        })
        layer.close(index);
      });
    }

    /**
     * 表格右侧操作按钮
     */
    table.on('tool(sensitiveWordsTableFilter)',function (obj){
      if (obj.event === 'edit'){
        const url = "/sensitive/admin/sensitive-edit?sensitiveId="+obj.data.id
        Pt.openModalBox("编辑敏感词",url,"40%","40%");
      }
      if (obj.event === 'delete'){
        var ids = [obj.data.id];
        deleteSensitive(ids);
      }
    })

    /**
     *  改变敏感词状态
     * @param obj
     * @return
     */
    form.on('switch(switchTest)', function(obj){
      var enabled = (this.checked === true) ? 1 : -1;
      const formData = {
        id: this.value,
        enable: enabled
      }
      updateSensitive(formData);
    });

    /**
     * 更新敏感词状态
     * @param data
     * @return
     */
    function updateSensitive(data){
      $.ajax({
        url: "/sensitive/admin/updateById",
        contentType: "application/json",
        type: "post",
        dataType: "json",
        data: JSON.stringify(data),
        success: function (res) {
          console.log(res);
          if (res.state) {
            layui.table.reload('currentSensitiveWordsTable');
            layer.closeAll();
          } else {
            Pt.showErrorMsg(res.msg);
          }
        }
      })
    }
  })
</script>
</html>
